#_*_ coding: utf-8 _*_
import datetime,time
from django.shortcuts import render,HttpResponse,HttpResponseRedirect
from .models import Persondata,Register,Adress,Cart1,All_Shopping
from django.views.decorators.csrf import ensure_csrf_cookie,csrf_exempt
from django.db.models import Q
import time
def index(req):
    return render(req,"index.html")
def login(req):
    return render(req,"login.html")
def toregister(req):
    return render(req,"register.html")
# 注册页面的请求
@csrf_exempt
def register(req):
    name = req.POST.get("user")
    pwd = req.POST.get("pwd")
    if Register.objects.filter(name=name):
        return HttpResponse(True)
    else:
        Register.objects.create(name=name, password=pwd)
        return HttpResponse(False)
#登陆页面的请求
@csrf_exempt
def toindex(req):
    user = req.POST.get("user")
    pwd = req.POST.get("pwd")
    all = All_Shopping.objects.all()
    all2 = list(all)
    shop1= all2[-1]
    shop2 = all2[-2]
    shop3 = all2[-3]
    shop4 = all2[-4]
    shop5 = all2[-5]
    p=1
    if Register.objects.filter(name=user,password=pwd):
        resp = render(req,"toindex.html",locals())
        resp.set_cookie("is_cookie",user,max_age=60*60*60*24)  #设置cookie
        return resp
    else:
        return  render(req,"login.html",{"err":"用户名或密码输入错误"})
#返回首页
def is_cookie(req):
    user = req.COOKIES.get("is_cookie")
    p =req.GET.get('p')
    if user:
        all = All_Shopping.objects.all()
        all2 = list(all)
        shop1 = all2[-1]
        shop2 = all2[-2]
        shop3 = all2[-3]
        shop4 = all2[-4]
        shop5 = all2[-5]
        return render(req,"toindex.html",locals())
    else:
        return render(req,"login.html")
#去个人资料的页面
def toinformation(req):
    user = req.GET.get("user")
    return render(req, "information.html",locals())
#个人资料页面请求
@csrf_exempt
def information(req):
    name = req.POST.get("name")
    sex = req.POST.get("sex")
    age = req.POST.get("age")
    date = req.POST.get("date")
    love = req.POST.get("love")
    user = req.POST.get("user")
    queryset = Register.objects.filter(name=user)
    for i in queryset:
        Persondata.objects.create(name=name,sex=sex,age=age,date=date,insteret=love,register_id=i.id)
    return render(req,"login.html",locals())
#修改信息页面
def update_information(req):
    user = req.GET.get("user")
    p = req.GET.get('p')
    object1 = Register.objects.get(name=user)
    object2 = Persondata.objects.get(register_id=object1.id)
    name = object2.name
    sex = object2.sex
    age = object2.age
    date = object2.date
    date = str(date)
    date1 = date.replace("-","/")
    insteret = object2.insteret
    return render(req, "update_information.html",locals())
#确认修改
def okinformation(req):
    p=1
    user = req.POST.get("user")
    object1 = Register.objects.get(name=user)
    object2 = Persondata.objects.get(register_id=object1.id)
    name = object2.name
    sex = object2.sex
    age = object2.age
    date = object2.date
    insteret = object2.insteret
    return render(req, "okupdate_information.html", locals())

def save(req):
    p = req.GET.get("p")
    name = req.POST.get("name")
    sex = req.POST.get("sex")
    age = req.POST.get("age")
    date = req.POST.get("date")
    love = req.POST.get("love")
    user = req.POST.get("user")
    queryset = Register.objects.filter(name=user)
    for i in queryset:
        Persondata.objects.filter(register_id=i.id).update(name=name, sex=sex, age=age, date=date, insteret=love, register_id=i.id)
    return render(req, "toindex.html", locals())
#分页方法
@csrf_exempt
def fenye(req,user,i,p):
    p=int(p)
    per_page_count = 3
    list2 = Adress.objects.filter(register_id=i.id)
    print(list2)
    list3 = list2.count()
    print(list3)
    if list3%per_page_count == 0:
        page = int(list3/per_page_count)
    else:
        page = int(list3//per_page_count) + 1
    start = (p - 1) * per_page_count
    end = p * per_page_count
    list4 = list2[start:end]
    if end >= list3:
        p1 = p
    else:
        p1 = p + 1
    if start < 1:
        p2 = p
    else:
        p2 = p - 1
    return render(req,"address.html",locals())
#收货地址
def address(req):
    user = req.GET.get("user")
    i = Register.objects.get(name=user)
    p = req.GET.get('p')
    print("11111", user, i, p)
    return fenye(req,user,i,p)
#跳到新增收货地址
def add_new_address(req):
    user = req.GET.get('user')
    p = req.GET.get('p')
    return render(req,"add_address.html",locals())
#将收货地址存入数据库中
@csrf_exempt
def add_address(req):
    name = req.POST.get("name")
    address = req.POST.get("address")
    phone = req.POST.get("phone")
    email = req.POST.get("email")
    user = req.POST.get("user")  #用户名
    i = Register.objects.get(name=user)
    p = req.GET.get('p')
    Adress.objects.create(name=name, adress=address, phone=phone, email=email, register_id=i.id)
    print("11111",user,i,p)
    return fenye(req, user, i, p)
#跳转到修改收货地址页面
def to_update_add_address(req):
    user = req.GET.get('user')
    id = req.GET.get("id")
    p = req.GET.get('p')
    querySet = Adress.objects.filter(id=id)
    return render(req,"update_add_address.html",locals())
#将修改的收货地址存入数据库中
@csrf_exempt
def update_add_address(req):
    p = 1
    name = req.POST.get("name")
    address = req.POST.get("address")
    phone = req.POST.get("phone")
    email = req.POST.get("email")
    user = req.POST.get("user")  #用户名
    id = req.POST.get("id")
    i = Register.objects.get(name=user)
    Adress.objects.filter(id=id).update(name=name,adress=address,phone=phone,email=email)
    return fenye(req, user, i, p)
#删除数据库中的地址
@csrf_exempt
def remove_address(req):
    p = 1
    user = req.GET.get("user")  #用户名
    id = req.GET.get("id")
    i = Register.objects.get(name=user)
    Adress.objects.filter(id=id).delete()
    return fenye(req, user, i, p)
#我的京东
def admin(req):
    return render(req,"admin.html")
#商品
def shopping(req):
    user = req.GET.get("user")
    return render(req,"shopping.html",locals())
#上传页面
def toshangchuan(req):
    return render(req,"file.html")
#上传商品信息
@csrf_exempt
def shangchuan(req):
    image = req.FILES.get("img")
    name = req.POST.get("name")
    price = float(req.POST.get("price"))
    desc = req.POST.get("desc")
    All_Shopping.objects.create(name=name, image=image, price=price, desc=desc)
    all_shop = All_Shopping.objects.all()
    print(all_shop)
    return look_all_shop(req)
#往购物车数据库中加数据
@csrf_exempt
def cart(req):
    user = req.POST.get("user")
    s2 = req.POST.get("s2")
    print(s2)
    shopping = All_Shopping.objects.get(image=s2)
    i = Register.objects.get(name=user)
    Cart1.objects.create(image=s2,name=shopping.name,price=shopping.price,desc=shopping.desc,register_id=i.id)
    return HttpResponse(user)
#购物车加入成功后返回的页面
def add_cart_success(req):
    user = req.GET.get("user")
    return render(req,'add_cart_success.html',locals())
#返回并展示购物车内容
def tocart(req):
    user = req.GET.get("user")
    i = Register.objects.get(name=user)
    querySet = Cart1.objects.filter(register_id=i.id)
    return render(req,"cart.html",locals())
#搜索并展示所有数据在页面上
@csrf_exempt
def show_shopping(req):
    name=req.POST.get("search")
    user=req.GET.get("user")
    p=1
    if name:
        querySet = All_Shopping.objects.filter(Q(name__icontains=name)).all()
        return render(req, "show_all_shopping.html", locals())
    else:
        return render(req,"toindex.html",locals())
#管理员页面
def  manage(req):
    register_set = Register.objects.all()
    return render(req,"mange.html",locals())
#管理员查看用户信息
def chakan_personal(req):
    name = req.GET.get('name')
    i = Register.objects.get(name=name)
    persondata = Persondata.objects.get(register_id=i.id)
    return render(req,'mange.html',locals())
#管理元查看用户的收货地址信息
def personal_address(req):
    name = req.GET.get('name')
    i = Register.objects.get(name=name)
    address = Adress.objects.filter(register_id=i.id)
    return render(req,'mange.html',locals())
#管理员查看用户下的购物车信息
def personal_shop(req):
    name = req.GET.get('name')
    i = Register.objects.get(name=name)
    cart = Cart1.objects.filter(register_id=i.id)
    return render(req,'mange.html',locals())
#管理员查看所有商品信息
def look_all_shop(req):
    all = All_Shopping.objects.all()
    return render(req,'look_all_shop.html',locals())
#管理员添加商品信息
def add_shop_card(req):
    return render(req,'file.html')
#管理员登陆页面
def tomanage_indes(req):
    return render(req,'manage_index.html')
#管理员登陆
def manage_jinru(req):
    name = req.POST.get('name')
    password = req.POST.get('password')
    if name == "admin" and password == "123":
        return look_all_shop(req)
    else:
        return render(req,'manage_index.html')
